package com.gfxy.modules.common.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.common.entity.MemberList;
import com.gfxy.modules.common.service.IMemberListService;
import com.gfxy.modules.system.entity.SysDepart;
import com.gfxy.modules.system.entity.SysUser;
import com.gfxy.modules.system.service.ISysDepartService;
import com.gfxy.modules.system.service.ISysUserDepartService;
import com.gfxy.modules.system.service.ISysUserDepartTeachingService;
import com.gfxy.modules.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: lijixin 2021/6/29 4:48 下午
 */
@Api(tags="成员列表")
@RestController
@RequestMapping("common/memberList")
@Slf4j
public class MemberListController extends JeecgController<MemberList, IMemberListService> {

    @Autowired
    private IMemberListService memberListService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysUserDepartService userDepartService;

    @Autowired
    private ISysDepartService departService;

    @Autowired
    private ISysUserDepartTeachingService sysUserDepartTeachingService;
    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "创新创业大赛-分页列表查询")
    @ApiOperation(value="创新创业大赛-分页列表查询", notes="创新创业大赛-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(MemberList memberList,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="50") Integer pageSize,
                                   HttpServletRequest req) {

        QueryWrapper<MemberList> queryWrapper = QueryGenerator.initQueryWrapper(memberList, req.getParameterMap());
        Page<MemberList> page = new Page<>(pageNo, pageSize);
        IPage<MemberList> pageList = memberListService.page(page, queryWrapper);
        System.out.println(pageList);
        return Result.OK(pageList);
    }

    @AutoLog(value = "成员列表-添加")
    @ApiOperation(value="成员列表-添加", notes="成员列表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody MemberList memberList) {
        memberListService.save(memberList);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "成员列表-编辑")
    @ApiOperation(value="成员列表-编辑", notes="成员列表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody MemberList gfxyTMemberList) {
        memberListService.updateById(gfxyTMemberList);
        return Result.OK("编辑成功!");
    }
    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "成员列表-通过id删除")
    @ApiOperation(value="成员列表-通过id删除", notes="成员列表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        memberListService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param main_id
     * @return
     */
    @AutoLog(value = "成员列表通过主表ID查询")
    @ApiOperation(value="成员列表主表ID查询", notes="成员列表-通主表ID查询")
    @GetMapping(value = "/queryMemberListByMainId")
    public Result<?> queryMemberListByMainId(@RequestParam(name="main_id",required=true) String main_id) {
        List<MemberList> memberListList = memberListService.selectByItemId(main_id);
        return Result.OK(memberListList);
    }

    /**
     * 通过realName模糊查询
     */
    @AutoLog(value = "成员列表通过主表ID查询")
    @ApiOperation(value="成员列表主表ID查询", notes="成员列表-通主表ID查询")
    @GetMapping(value = "/queryUserLikeRealName")
    public Result<?> queryMemberListListByMainId(@RequestParam(name="realName",required=true) String realName) {
        List<SysUser> userList = memberListService.selectUserLikeRealName(realName);
        return Result.OK(userList);
    }

    @AutoLog(value = "查询所有教师")
    @ApiOperation(value="查询所有教师", notes="查询所有教师")
    @GetMapping(value = "/queryAllUser")
    public Result<?> queryAllUser() {
        List<SysUser> userList = memberListService.selectAllUser();
        return Result.OK(userList);
    }

//
    /**
     //     *   添加
     //     *
     //     * @param HttpServletRequest request
     //     * @return
     //     */
//    @AutoLog(value = "成员列表-添加")
//    @ApiOperation(value="成员列表-添加", notes="成员列表-添加")
//    @PostMapping(value = "/add")
//    public Result<?> add(@RequestBody MemberList memberList) {
//        System.out.println("======================>>>>>>>>>LLLLL::::::" + memberList);
//        SysUser user = userService.getUserByName(memberList.getTeacherId());
//
//        System.out.println(user);
//
////        List<DepartIdModel> departIdModels = userDepartService.queryDepartIdsOfUser(user.getId());
//        List<SysDepart>  sysDeparts = departService.queryUserDeparts(user.getId());
//        System.out.println("<<<<<<" + sysDeparts);
//        memberList.setOrgId(sysDeparts.get(0).getOrgCode());
//        System.out.println("======================>>>>>>>>>LLLLL::::::" + memberList);
//        memberListService.save(memberList);
//        return Result.OK("添加成功！");
//    }
    @AutoLog(value = "成员列表-通过UserName查询部门数据")
    @ApiOperation(value="成员列表-通过UserName查询部门数据", notes="成员列表-通过UserName查询部门数据")
    @GetMapping(value = "/queryDepartmentByUserName")
    public Result<?> queryDepartmentByUserName(@RequestParam(name="userName",required=true) String userName) {
        SysUser user = userService.getUserByName(userName);
        if(user == null){
            return Result.error("未找到用户数据");
        }
        List<SysDepart> sysDeparts = departService.queryUserTeachingDeparts(user.getId());

        if(sysDeparts==null || sysDeparts.size() ==0) {
            return Result.error("未找到部门数据");
        }
        if(sysDeparts.size()!=1){
            return Result.error("部门数据不唯一");
        }
        return Result.OK(sysDeparts.get(0));
    }
}
